import React, { Component } from 'react';
import { Table } from 'antd';
import * as config from '../../../utils/config';
import styles from '../../../index.less';

class JurGroupData extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sActiveId: props.sActiveId, /* 窗体Id */
      dispatch: props.dispatch,
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      gdsformconst: props.gdsformconst, /* 常量 */
      jurColumn: [],
      jurData: [],
      currentKey: props.currentKey, /* 选中当前的行 */
    };
  }

  componentWillMount() {
    /* 获取表数据 */
    const {
      sActiveId, dispatch, sTabId, componentName, gdsformconst,
    } = this.state;
    if (this.state.jurColumn.length === 0) {
      const column = gdsformconst.filter(item => item.sName === 'jurGroup')[0];
      const jurColumn = [{
        title: column.showName,
        dataIndex: column.sName,
        key: column.sName,
      }];
      this.setState({ jurColumn });
    }
    const dataUrl = `${config.server_host}sysjurisdiction/getGroupData?sModelsId=${sActiveId}`;
    dispatch({ type: 'content/getUrlData', payload: { url: dataUrl, sTabId, componentName } });
  }

  componentWillReceiveProps(nextProps) {
    const { dataList } = nextProps.content;
    const { sDataListTabId, loading, componentName } = dataList;
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (!loading) {
        const { urlData } = dataList;
        const jurData = [];
        urlData.forEach((child) => {
          jurData.push({
            key: child.sId,
            jurGroup: child.sName,
          });
        });
        this.setState({ jurData });
      }
    }
    this.setState({ currentKey: nextProps.currentKey });
  }

  handleClick = (record) => {
    const { key } = record;
    this.props.onGroupsId(key);
  };

  render() {
    const { jurColumn, jurData, currentKey } = this.state;
    return (
      <Table
        bordered
        columns={jurColumn}
        dataSource={jurData}
        size="small"
        pagination={false}
        scroll={{ y: 220 }}
        onRow={(record) => {
          return { onClick: () => { this.handleClick(record); } };
        }}
        rowClassName={(record) => {
          if (record.key === currentKey) {
            return `${styles.clickRow}`;
          } else {
            return '';
          }
        }}
      />
    );
  }
}

export default JurGroupData;
